package com.qiji.video.config;

import com.qiji.video.web.interceptor.JwtInterceptor;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class ShiroConfig {

    // 自定义realm
    @Bean
    public MyRealm myRealm(){
        return new MyRealm();
    }

    // shiro安全管理器
    @Bean
    public SecurityManager securityManager(@Autowired MyRealm myRealm){
        return new DefaultWebSecurityManager(myRealm);
    }

    // shiro过滤器
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager);
        // 设置过滤器并添加
        HashMap shiroFilter = new HashMap<>();
        shiroFilter.put("jwt",new JwtInterceptor());
        factoryBean.setFilters(shiroFilter);

        // 添加拦截的路径
        HashMap<String,String> map = new HashMap<>();
        map.put("/auth/user/**","jwt");
        map.put("/auth/user/**","jwt");
//        map.put("/front/**","jwt");
        map.put("/auth/admin/**","jwt");
        map.put("/auth/super/**","jwt");
        factoryBean.setFilterChainDefinitionMap(map);
        return factoryBean;
    }
}
